<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="order_number" label="订单编号">
        <uni-easyinput placeholder="订单编号，系统自动生成" v-model="formData.order_number"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="user_id" label="用户id" required>
        <uni-easyinput placeholder="提交用户的ID，参考uni-id-users表" v-model="formData.user_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="create_time" label="">
        <uni-datetime-picker return-type="timestamp" v-model="formData.create_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="job_type_id" label="工种类型" required>
        <uni-easyinput v-model="formData.job_type_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="employment_time" label="用工时间" required>
        <uni-datetime-picker return-type="timestamp" v-model="formData.employment_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="address" label="地址" required>
        <uni-easyinput v-model="formData.address"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="longitude" label="经度">
        <uni-easyinput v-model="formData.longitude"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="latitude" label="纬度">
        <uni-easyinput v-model="formData.latitude"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="house_number" label="门牌号">
        <uni-easyinput v-model="formData.house_number"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="charging_mode" label="收费模式">
        <uni-data-checkbox v-model="formData.charging_mode" :localdata="formOptions.charging_mode_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="require_peoples" label="需要人数">
        <uni-easyinput type="number" v-model="formData.require_peoples"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="male_numbers" label="男工人数">
        <uni-easyinput type="number" v-model="formData.male_numbers"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="female_numbers" label="女工人数">
        <uni-easyinput type="number" v-model="formData.female_numbers"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="require_work_time" label="需要工作时间">
        <uni-easyinput type="number" v-model="formData.require_work_time"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="charging_unit" label="计费单位">
        <uni-data-checkbox v-model="formData.charging_unit" :localdata="formOptions.charging_unit_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="offer" label="出价">
        <uni-easyinput type="number" v-model="formData.offer"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="supplement" label="补充文本">
        <uni-easyinput type="textarea" v-model="formData.supplement"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="images" label="补充图片">
        <uni-file-picker file-mediatype="image" return-type="array" v-model="formData.images"></uni-file-picker>
      </uni-forms-item>
      <uni-forms-item name="status" label="">
        <uni-data-checkbox v-model="formData.status" :localdata="formOptions.status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/order.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'order';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "order_number": "",
        "user_id": "",
        "create_time": null,
        "job_type_id": "",
        "employment_time": null,
        "address": "",
        "longitude": "",
        "latitude": "",
        "house_number": "",
        "charging_mode": 0,
        "require_peoples": null,
        "male_numbers": null,
        "female_numbers": null,
        "require_work_time": null,
        "charging_unit": 0,
        "offer": null,
        "supplement": "",
        "images": [],
        "status": 0
      }
      return {
        formData,
        formOptions: {
          "charging_mode_localdata": [
            {
              "text": "自己出价",
              "value": 0
            },
            {
              "text": "计时",
              "value": 1
            }
          ],
          "charging_unit_localdata": [
            {
              "text": "小时",
              "value": 0
            },
            {
              "text": "天",
              "value": 1
            }
          ],
          "status_localdata": [
            {
              "text": "进行中",
              "value": 0
            },
            {
              "text": "待支付",
              "value": 1
            },
            {
              "text": "待评价",
              "value": 2
            },
            {
              "text": "已完成",
              "value": 3
            },
            {
              "text": "已取消",
              "value": 4
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
